package com.dm0858.bianmin.mine.base;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;


public abstract class BaseRecyclerViewAdapter<D extends RecyclerView.ViewHolder> extends RecyclerView.Adapter<BaseViewHolder> {

    /**
     * 设置item数量
     */
    protected int i;

    /**
     * 数据源
     */
    protected List<D> data;

    /**
     * 布局资源
     */
    @LayoutRes
    protected int resource;

    private Context mContext;
    private LayoutInflater mLayoutInflater;


    /**
     * 构造方法
     * @param mContext
     */
    public BaseRecyclerViewAdapter(Context mContext, @LayoutRes int resource, List<D> data){
        this.mContext = mContext;
        this.mLayoutInflater = LayoutInflater.from(mContext);
        this.data = data == null ? new ArrayList<D>() : data;
        if (resource != 0){
            this.resource = resource;
        }
        else {
            throw new NullPointerException("请设置item资源文件id");
        }
    }


    @NonNull
    @Override
    public BaseViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = mLayoutInflater.inflate(resource,null);
        return new BaseViewHolder(mContext,view);
}

    @Override
    public void onBindViewHolder(@NonNull BaseViewHolder viewHolder, int i) {
        bindData(viewHolder,i);
    }

    /**
     * 绑定数据
     * @param holder
     * @param position
     */
    protected abstract void bindData(BaseViewHolder holder, int position);

    @Override
    public int getItemCount() {
        return getSize();
    }

    protected abstract int getSize();

}
